# -*- coding: UTF-8 -*-
'''
@Project: yc-stock
@File: Index.py
@Notes: 
@Author: zhuyoucheng
@Date: 2021/3/20 0020 11:51
'''

from App.Middleware.BaseMiddleware import BaseMiddleware
from App.Model.PyMysqlTest import PyMysqlTest
from App.Model.PyMysql import PyMysql
# from App.Model.StockCodeModel import StockCodeModel
# import baostock as bs
# import pandas as pd
import datetime


class Index(BaseMiddleware):

    def login(self):

        pass

    def save_code_data(self):
        today = datetime.date.today()
        oneday = datetime.timedelta(days=1)
        date = today - oneday
        lg = bs.login()
        # 显示登陆返回信息
        print('login respond error_code:' + lg.error_code)
        print('login respond  error_msg:' + lg.error_msg)

        # 获取指定日期的指数、股票数据
        stock_rs = bs.query_all_stock(date)
        stock_df = stock_rs.get_data()
        data_df = pd.DataFrame()
        code_params = []


        for value in stock_df:
            print('---------------------')
            print(value)

            # stock_code = value["code"]
            # code_name  = value["code_name"]
            # print(stock_code+code_name)

        # for code in stock_df["code"]:
        #     print(code)
        #     k_rs = bs.query_history_k_data_plus(code, "date,code,open,high,low,close", date, date)
        #     data_df = data_df.append(k_rs.get_data())
        #     code_val = {"stock_code": code, 'type': 1}
        #     # 保存代码
        #     code_params.append(code_val)
        #
        # StockCodeModel().create(code_params)

    def download_data(self, date):
        lg = bs.login()
        # 显示登陆返回信息
        print('login respond error_code:' + lg.error_code)
        print('login respond  error_msg:' + lg.error_msg)

        # 获取指定日期的指数、股票数据
        stock_rs = bs.query_all_stock(date)
        stock_df = stock_rs.get_data()
        data_df = pd.DataFrame()
        code_params = []
        for code in stock_df["code"]:
            print(code)
            k_rs = bs.query_history_k_data_plus(code, "date,code,open,high,low,close", date, date)
            data_df = data_df.append(k_rs.get_data())
            code_val = {"stock_code": code, 'type': 1}
            #保存代码
            code_params.append(code_val)

        table = 'stock_code'
        PyMysql().create(code_params, table)
        bs.logout()

    def batch_save_data(self):
        data = {'stock_code': 'fdsf', 'code_name': '爱尚国际', 'trend': 40, 'type': 1}
        list = []
        for i in range(4):
            list.append(data)
        table = 'sz_50_stock_code'
        PyMysqlTest(table=table).create_batch(list)